// store.js
"use client"
import { makeAutoObservable, action, autorun } from 'mobx';
import {message} from "antd";
import {fetch} from '../utils/request';

class CounterStore {
    count = 0;
    total =  0;
    pageNum =  1;
    pageSize =  10;
    mockFoods = [];
    mockGames = [];

    constructor() {
        makeAutoObservable(this, {
            increment: action.bound
        });
    }
    init(){
        // fetch('/api3/users/getFoods', {}).then((res) => {
        //     console.log('示例页面的初始化事件', res);
        //     if (res && res.errCode === 0){
        //         this.mockFoods = res.msg;
        //     }
        // })
        this.getFoods();
        this.getGames();
    };
    getFoods = async (params = { pageNum: 1, pageSize: 10 }) => {
        const res = await fetch('api3/users/searchFoods', {
            method: 'POST',
            data: params
        });
        if (res && res.errCode === 0) {
            const { list = [], pagination } = res.data || {};
            this.setFoods(list);
            this.total = (pagination?.total || 0);
            this.pageNum = params?.pageNum || 1;
            this.pageSize = params?.pageSize || 10;
        }
    };
    getGames(){
        fetch('/api3/games/getGames', {method: 'GET', params: {page: 1, pageSize: 10} }).then((res) => {
            console.log('示例页面的初始化事件', res);
            if (res && res.errCode === 0){
                this.mockGames = res.msg;
            }
        })
    };
    addFood(){
        fetch('/api3/users/addFood', { method: 'POST', data: {
            name: '1'
            }}).then((res) => {
            if (res && res.errCode === 0){
                this.getFoods();
            }
        })
    }
    delFood(props){
        console.log('删除传值', props);
        const {id} = props;
        fetch('/api3/users/delFood', { method: 'POST', data: {id}}).then((res) => {
            console.log('删除的回调', res);
            if (res && res.errCode === 0){
                // message.success('删除成功');
                this.getFoods();
            }
        })
    }
    increment() {
        this.count++;
    }

    decrement() {
        this.count--;
    }
//     autorun(() => {
//     console.log('counter', counter.count);
// })

    setFoods(foods) {
        this.mockFoods = foods;
    }

}

export default new CounterStore();
